package com.csw.shuanfa.bitset;

import com.csw.shuanfa.entity.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class StreamUserTest {
    /**
     * https://www.jb51.net/article/193458.htm
     *
     * @param args
     */
    public static void main(String[] args) {
        List<User> list = new ArrayList();
        list.add(new User("张三", 1, null));
        list.add(new User("张三", 2, "接招"));
        list.add(new User("李四", 3, null));

        /**
         * 1.list 转list,不含返回值
         */
        List<String> strList = list.stream().map(c -> c.getName()).collect(Collectors.toList());
        //strList]" + strList);
        /**
         * 1.2 list 转list 含返回值
         */
        List<User> userList = list.stream().map(user -> {
            User user1 = new User();
            user1 = user;
            user1.setName(user1.getName() + "*");
            return user1;
        }).collect(Collectors.toList());
        //userList]" + userList);
        /**
         * 2 . list转map
         */
        Map<Integer, User> longStringMap = list.stream().collect(Collectors.toMap(User::getId, s -> s));
        //longStringMap]" + longStringMap);
        /**
         * 2 . list转map,key重复问题
         */
        Map<String, User> longStringMap2 = list.stream().collect(Collectors.toMap(User::getName, s -> s, (u1, u2) -> u2));
        //longStringMap2]" + longStringMap2);
        /**
         * 2 . list转map,key为null的问题
         */
        Map<String, User> longStringMap3 = list.stream().collect(Collectors.toMap(User::getStatus, s -> s, (u1, u2) -> u2));
        //longStringMap3]" + longStringMap3);
        /**
         * 2 . list转map,多个值为空,
         * 只要值为空就报错，要过滤
         */
        Map<String, String> longStringMap4 = list.stream().filter(c -> c.getStatus() != null).collect(Collectors.toMap(User::getName, User::getStatus));
        //longStringMap4]" + longStringMap4);
        /**
         * 3.分组
         */
        Map<String, List<User>> stringListMap = list.stream().collect(Collectors.groupingBy(User::getName));
        //stringListMap]" + stringListMap);
        /**
         * 3.2 分组统计计算
         */
        Map<String, Integer> integerIntegerMap = list.stream().collect(Collectors.groupingBy(User::getName, Collectors.summingInt(User::getId)));
        //integerIntegerMap]" + integerIntegerMap);
        /**
         * 3.2 分组统计计算,加上筛选条件
         */
        Map<String, Integer> integerIntegerMap2 = list.stream().filter(c -> c.getId() < 3).collect(Collectors.groupingBy(User::getName, Collectors.summingInt(User::getId)));
        //integerIntegerMap2]" + integerIntegerMap2);

        Map map = new HashMap();
        map.put(null, null);
        map.put("aa", null);
        map.put("bb", null);
        //map]" + map);
    }
}
